<?php
class Perawatan{
	public $no_pancang; // id
	
	public $jenis_material;
	public $jenis_perawatan;
	
	public $perawatan_bulanan;	
	public $perawatan_material;
	public $perawatan_jenis;
	
	public $perawatan_frek_pupuk;
	public $perawatan_frek_jenis;
	
	private $db;
	private $no_individu;
	private $year;
	
	function Perawatan($db, $no_pancang, $year=2000){
		$this->db 		  = $db;
		$this->no_pancang = $no_pancang;
		$this->year		  = $year;
		$this->jenis_material = array('pupuk', 'insektisida', 'herbisida', 'fungisida', 'bahan_kimia_lain', 'peralatan') ;
		
		$this->process();
	}
	
	private function getPerawatanFrekuensiPupuk(){
		$perawatan_frek_pupuk = array();
		
		foreach($this->perawatan_material['pupuk'] as $idx=>$jml_pemupukan){
			if( $jml_pemupukan > 0 ){
				$bulan = $idx+1;
				$query = "
                    SELECT 
                        `nama` 
                    FROM 
                        `caramel_operational`.`perawatan` 
                    INNER JOIN `caramel_managerial`.`perawatan_pupuk` 
                        ON perawatan.Id=perawatan_pupuk.perawatan_id 
                    WHERE perawatan.no_individu='".$this->no_individu."' 
                        AND MONTH(tgl_perawatan)='".$bulan."' 
                        AND YEAR(tgl_perawatan)='".$this->year."'";
				$temp  = $this->db->get_results($query, ARRAY_A);
				
				if( is_array($temp) ){
					foreach($temp as $d){
						$perawatan_frek_pupuk[$idx][] = $d['nama'];
					}
				}
			}
		}
		
		return $perawatan_frek_pupuk;
	}
	
	private function getPerawatanFrekuensiJenis(){
		$perawatan_frek_jenis = array();
		
		$query = "SELECT month(tgl_perawatan) as bulan, jenis_perawatan FROM `perawatan`.perawatan WHERE YEAR(tgl_perawatan)='".$this->year."' AND no_individu='".$this->no_individu."'";
		
		$temp  = $this->db->get_results($query, ARRAY_A);
		
		if( is_array($temp) ){
			foreach($temp as $d){
				$perawatan_frek_jenis[$d['bulan']-1][] = $d['jenis_perawatan'];
			}
		}
		
		return $perawatan_frek_jenis;
	}
	
	private function getNoIndividu(){
		$query 		 = "SELECT `no_individu` FROM `pokok`.`biodata_tanaman` WHERE `no_pancang`='".$this->no_pancang."'";
		$no_individu = $this->db->get_var($query); 
		
		return $no_individu;
	}
	
	private function getPerawatanBulanan(){
		$perawatan_bulanan = array();
		
		$query = "SELECT MONTH(tgl_perawatan) as bulan, COUNT(*) as jml_perawatan FROM `perawatan`.`perawatan` WHERE `no_individu`='".$this->no_individu."' AND YEAR(tgl_perawatan)='".$this->year."'GROUP BY MONTH(tgl_perawatan)";
		$temp  = $this->db->get_results($query, ARRAY_A);
		 
		if( $temp ){
			foreach($temp as $d){
				$perawatan_bulanan[$d['bulan']-1] = $d['jml_perawatan'];
			}
		}
		else{
			$perawatan_bulanan = array_fill(0,12,0);
		}
		
		return $perawatan_bulanan;
	}
	
	private function getPerawatanJenisMaterial(){
		$perawatan_material = array();
		
		foreach($this->jenis_material as $material){
			$table = 'perawatan_'.$material;
			
			$query = "
				SELECT count(*) as jml_perawatan, MONTH(tgl_perawatan) as bulan
				FROM `perawatan`.perawatan INNER JOIN `perawatan`.`".$table."` ON perawatan.Id=$table.perawatan_id WHERE no_individu = '".$this->no_individu."' AND YEAR(tgl_perawatan)='".$this->year."'
				GROUP BY MONTH(tgl_perawatan)";
			 
			$temp = $this->db->get_results($query, ARRAY_A);
			
			if( is_array($temp) AND count($temp) > 0 ){
				for($i=1; $i<=12; $i++){
					$jml_perawatan = $temp[$i-1]['jml_perawatan'] > 0 ? $temp[$i-1]['jml_perawatan'] : 0;
					$perawatan_material[$material][$i-1] = $jml_perawatan;
				}
			} 
			else{
				$perawatan_material[$material] = array_fill(0,12,0);
			}
		}

		return $perawatan_material;
	}
	
	private function getPerawatanJenisPerawatan(){
		$perawatan_jenis = array();
		
		$query = "SELECT DISTINCT jenis_perawatan FROM perawatan";
		$temp  = $this->db->get_results($query, ARRAY_A);
		foreach($temp as $d){
			$perawatan_jenis[]=$d['jenis_perawatan'];
		}
		
		$query = "SELECT COUNT(*) as jml_perawatan, jenis_perawatan FROM `perawatan` WHERE `no_individu`='".$this->no_individu."' AND YEAR(tgl_perawatan)='".$this->year."' GROUP BY jenis_perawatan";
		$temp  = $this->db->get_results($query, ARRAY_A);
		$perawatan_jenis = array_combine($perawatan_jenis, array_fill(0,count($perawatan_jenis),0));
		
		if( $temp ){
			foreach($temp as $d){
				$perawatan_jenis[$d['jenis_perawatan']] = $d['jml_perawatan'];
			}
		}
		else{
			foreach($this->jenis_material as $m){
				$perawatan_jenis[$m] = 0;
			}
		}		
		
		return $perawatan_jenis;
	}
	private function process(){
		$this->no_individu = $this->getNoIndividu();
		
		if( $this->no_individu ){			
			$this->perawatan_bulanan    = $this->getPerawatanBulanan();
			$this->perawatan_material   = $this->getPerawatanJenisMaterial();
			$this->perawatan_jenis		= $this->getPerawatanJenisPerawatan();			
			$this->perawatan_frek_pupuk = $this->getPerawatanFrekuensiPupuk();
			$this->perawatan_frek_jenis = $this->getPerawatanFrekuensiJenis();
			print '<pre>'; print_r($this->perawatan_frek_jenis); 		
		}	
	}
}
?>